home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cuj9205.zip / 1005056A < prev    next >
Text File  |  1992-06-02  |  4KB  |  192 lines

  1. /* Listing 1 */
  2.  
  3. /*****************************************************
  4.     ADCTEST.C 
  5.  
  6.     Application for high speed data acquisiton using 
  7.     DMA on the Lab Master AD.
  8.  
  9.     Copyright Don Bradley, 1991.
  10.  
  11.     Permission is granted for used of these routines
  12.     in any manner as long as this copyright notice is
  13.     included.
  14.  
  15.     Tested using Quick C 2.5 and MSC 6.0 on a 
  16.     Toshiba T5200.
  17.  
  18.  *****************************************************/
  19.  
  20. #include <math.h>
  21. #include <conio.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <process.h>
  25. #include <fcntl.h>
  26. #include <io.h>
  27. #include <sys\types.h>
  28. #include <sys\stat.h>
  29. #include <malloc.h>
  30.  
  31. #include "labmastr.h"
  32. #include "dma.h"
  33.  
  34. #define ESC 0x1B
  35. #define FILEBUFFSIZE 0x4000
  36.  
  37. #define OK_EXIT                     0
  38. #define MISSING_PARAMETERS_ERROR     1
  39. #define NOT_ENABLED_ERROR             2
  40. #define CANT_OPEN_FILE_ERROR         3
  41. #define CANT_INITIALIZE_ERROR         4
  42.  
  43. void main(int argc, char *argv[]);
  44. void dma_handler(int value);
  45.  
  46. static int file_buffer[FILEBUFFSIZE];
  47. static int file_handle;
  48. static int file_buff_ptr = 0;
  49. static int channel_buffer[MAX_CHANNELS];
  50. static int channel_number = 0;
  51.  
  52.  
  53. static double freq = 1000.0;
  54. static long num_of_samples = 6000L;
  55. static int dma_channel = 5;
  56. static int num_of_channels = 1;
  57.  
  58.  
  59. void main(int argc, char *argv[])
  60.     {
  61.     unsigned long num_collected = 0L;
  62.     long loop_count = 0l;
  63.     int ch;
  64.  
  65.     if(argc < 4){
  66.         printf("Format:\n\n");
  67.         printf("    ADCTEST <number of channels> ");
  68.         printf("<number of samples> <sampling ");
  69.         printf("frequency (Hz)>\n\n");
  70.         exit(MISSING_PARAMETERS_ERROR);
  71.         }
  72.  
  73.     num_of_channels = atoi(argv[1]);
  74.     num_of_samples = atol(argv[2]);
  75.     freq = atof(argv[3]);
  76.  
  77.     if (!LabMasterAD_Enable()) {
  78.         printf("LabMasterAD %u V%u was not enabled.\n",
  79.              LabMasterAD_Product(), 
  80.              LabMasterAD_Version());
  81.         exit(NOT_ENABLED_ERROR);
  82.         }
  83.     
  84.     printf("\n\nLabMasterAD %u", LabMasterAD_Product());
  85.     printf(" V%u Enabled for\n", LabMasterAD_Version());
  86.     printf("%d channels, ", num_of_channels);
  87.     printf("%ld samples at ", num_of_samples);
  88.     printf("at %lf Hz.\n", freq);
  89.  
  90.     if((file_handle = open("DATAFILE.DAT", O_BINARY | 
  91.           O_WRONLY | O_CREAT | O_TRUNC, S_IWRITE)) < 0){
  92.         printf("DATAFILE.DAT could not be opened.\n\n");
  93.         exit(CANT_OPEN_FILE_ERROR);
  94.         }
  95.  
  96.     timer_reset();
  97.     if(!init_adc_dma(num_of_channels, num_of_samples, 
  98.             &freq, dma_channel, dma_handler)){
  99.         printf("Errors initializing DMA transfer.\n\n");
  100.         exit(CANT_INITIALIZE_ERROR);
  101.         }
  102.  
  103.     printf("\nPress SPACE bar to start.\n\n");
  104.  
  105.     while ((ch = getch()) != ' ' && ch != 0x1b)
  106.         ;
  107.  
  108.     if(ch == ESC) {
  109.         printf("Execution Prematurly Terminated.\n\n");
  110.         exit(OK_EXIT);
  111.         }
  112.  
  113.     printf("ADC DMA has been started.\n");
  114.     printf("Press ESC to exit from ADC DMA.\n\n");
  115.  
  116.     enable_dma(dma_channel);
  117.     enable_adc();
  118.  
  119.     while (!adc_dma_done()) {
  120.         if (kbhit())
  121.             if (getch() == ESC)
  122.                 terminate_adc_dma();
  123.  
  124.         get_next_adc_values();
  125.         
  126.         /*
  127.         // Insert here required main loop processing.
  128.         {
  129.         int i;
  130.  
  131.         for(i=0; i<num_of_channels; i++)
  132.             printf("%4X    ", channel_buffer[i]);
  133.         for(i=0; i<num_of_channels; i++)
  134.             printf("\b\b\b\b\b\b\b\b");
  135.         }
  136.         */
  137.         
  138.         loop_count++;
  139.         }
  140.     
  141.     // Write remaining values in file buffer.
  142.     write(file_handle, file_buffer, file_buff_ptr*
  143.         sizeof(int));
  144.  
  145.     printf("\n\nADC DMA was initialized for\n");
  146.     printf("\t%d channels, %ld samples at %lf Hz\n\n",
  147.          num_of_channels, num_of_samples, freq);
  148.  
  149.     printf("    Status:%8X\n", adc_status());
  150.     printf(" Collected:%8ld\n", 
  151.         adc_dma_conversion_count());
  152.     printf("Loop Count:%8ld\n\n", loop_count);
  153.     printf("Finished...    ");
  154.     
  155.     if(adc_dma_conversion_count() < num_of_channels * 
  156.             num_of_samples)
  157.         printf("OVERRUN ERROR\n\n");
  158.     else
  159.         printf("NO ERRORS\n\n");
  160.     
  161.     LabMasterAD_Disable();
  162.     exit(OK_EXIT);
  163.     }
  164.  
  165. void dma_handler(int value)
  166. /*& DMA handler routine. Used for processing each piece
  167.      of    data. */
  168.     {
  169.     
  170.     file_buffer[file_buff_ptr] = value;
  171.  
  172.     if(++file_buff_ptr >= FILEBUFFSIZE) {
  173.         
  174.         write(file_handle, file_buffer, 
  175.             FILEBUFFSIZE*sizeof(int));
  176.         
  177.         file_buff_ptr = 0;
  178.         }
  179.     
  180.     channel_buffer[channel_number] = value;
  181.  
  182.     if(++channel_number >= num_of_channels)
  183.         channel_number = 0;
  184.  
  185.     // Insert here any further process that may be 
  186.     // required.
  187.     // Be carful that the new code does not cause the 
  188.     // duration of this routine to exceed the required 
  189.     // limits for the selected data acquisition 
  190.     // specification.
  191.     }
  192.